This lesson assumes you have read [[CodeGen|Lesson 1]], and are comfortable writing a driver class to generate your components.

== Caveat ==

When working with an IDE especially, it can be easy to create a circular dependency in your generated code, e.g. your component inherits from a base class that makes reference to a static method in a class the is a subclass of your component. In other words, you can't compile either one without the other.  It is important that you do a clean build at frequent intervals to detect these cycles early on.

== Maven ==

The Maven build process uses an ant build file to compile your specs and generate the code.  To use the build file, you need to configure the ant-run plug-in by adding the following to your pom.xml:
<<
   <build>
      <plugins>
         <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
               <execution>
                  <phase>generate-sources</phase>
                  <configuration>
                     <tasks>
                        <ant antfile="${basedir}/build.xml"
                           inheritRefs="true" target="generate" inheritAll="true"/>
                     </tasks>
                     <sourceRoot>
                        ${project.build.directory}/generated-sources/components
                     </sourceRoot>                  
                  </configuration>
                  <goals>
                     <goal>run</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>            
         ... other plugin config ...
      </plugins>
   </build>

>>

Of course, you'll have to change <code>sourceRoot</code> if you change the build file to output somewhere else.

== Ant ==

The following build file is setup for the default maven directories, so you'll likely need to make changes if you're not using maven.

<<
<?xml version="1.0"?>
<project name="components-core" default="generate">
	<!-- The term 'driver' refers to the class   -->
	<property name="src" value="src/main/java/" 
             description="The source directory containing your driver and its dependencies." />
	<!-- resources is the source dir where the template files live -->
	<property name="resources" value="src/main/resources/" />
	<property name="out" value="target/classes" />
	<!--You need to use the slash syntax (instead of .s), but not the .java extension -->
	<property name="driver" value="com/jsftoolkit/components/spec/GenerateComponents" />
	<!-- Directory where the jars are:
		At a minimum you will need:
		* el-api-1.0.jar
		* jsf-api-1.2.jar
		* jsp-api-2.1.jar
		* components-base.jar (the JSF Toolkit base jar)
	-->
	<property name="deps.dir" value="${basedir}/../deps/" />

	<!-- 
		Shouldn't need to change anything below this line.
	-->

	<path id="maven.dependency.classpath">
		<fileset dir="${deps.dir}" includes="*.jar" />
	</path>
	<path id="maven.compile.classpath" location="${out}" />
	<path path="${resources}" id="resources" />

	<target name="generate" depends="compileDriver">
		<java classname="${driver}" fork="true">
			<!-- 
				basedir is the directory root for the paths to your config files,
				if they are relative.
			-->
			<sysproperty key="jsftoolkit.generator.basedir" value="${basedir}" />
			<classpath refid="maven.dependency.classpath" />
			<classpath refid="maven.compile.classpath" />
			<classpath refid="resources" />
		</java>
	</target>

	<target name="clean">
		<delete dir="${out}" />
		<mkdir dir="${out}" />
	</target>

	<target name="compileDriver">
		<mkdir dir="${out}" />
		<javac srcdir="${src}" destdir="${out}" debug="on" fork="true">
			<include name="${driver}.java" />
			<classpath refid="maven.dependency.classpath" />
			<classpath refid="maven.compile.classpath" />
			<classpath refid="resources" />
		</javac>
	</target>
</project>

>>
The above depends on your driver class referencing the specification classes by their class constant, and not using <code>Class.forName</code> or something like that.  This way the java compiler will be able to properly resolve all the dependencies.

== Other Processes ==

To generate component code, all that is required is an instance of [javadoc/com/jsftoolkit/gen/info/ComponentInfo.html ^ComponentInfo^]. You can produce this instance however you like.  

We highly recommend the class of constants method.  It is type safe, refactors well, integrates more easily into the build process, and is fairly concise (for Java).  There is also an [javadoc/com/jsftoolkit/gen/XmlComponentInfoFactory.html ^XmlComponentInfoFactory^].  Note that when integrating the XML method into your build process, all dependent jars must still be in the class path as well as compiled versions of any special super classes before the generator can be run.